<?php 
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: list.phtml 4684 2010-08-16 08:44:14Z huuphuoc $
 * @since		2.0.0
 */
?>
<?php 
$this->headTitle()->prepend($this->translate('about_news_description'). ' - ');
$this->headScript()->appendFile($this->APP_STATIC_SERVER.'/public/js/jquery.json-2.3.min.js');
?>

<div id="r-heading">
	<img src="<?php echo $this->APP_STATIC_SERVER.'/skins/admin/default/images/article.png';?>" alt=""/>
	<span><?php echo $this->translate('article_list_page_title') ?></span>
</div>
<?php
//nếu có message thì hiển thị
if(!empty($this->message)){
	echo '<div id="message">'.$this->message.'</div>';
}
?>
<div>
	<form id="frmActions" action="<?php echo $this->url(array(), 'news_article_list'); ?>" method="post">
		<div id="actions">
			<!--<input type="text" id="txtKeyword" name="keyword" class="text" value="" size="25"/>
			<input type="submit" id="btnSearch" class="button" value="Tìm kiếm"/>  -->
			<input type="button" id="btnCreate" class="button" value="<?php echo $this->translate('article_add_page_title') ?>" onclick="location = '<?php echo $this->url(array(), 'news_article_add');?>';"/>
			<input type="button" id="btnDelete" class="button" value="<?php echo $this->translate('article_delete_many')?>"/>
			<input type="hidden" id="hddIDs" value="" name="ids"/>
		</div>
          <div id="filters">
                  <div class="f-data">
                      <label><?php echo $this->translate('article_list_search_keyword'); ?>:</label>
                      <input type="text" style="width: 180px" name="keyword" value="<?php if (isset($this->exp['keyword'])) : ?><?php echo $this->exp['keyword']; ?><?php endif; ?>" />
                  </div>
                  <div class="f-data">
                        <label><?php echo $this->translate('article_list_search_id'); ?>:</label>
				<input type="text" style="width: 180px" name="articleId" id="articleId" value="<?php if (isset($this->exp['id'])) : ?><?php echo $this->exp['id']; ?><?php endif; ?>" />
                  </div>
                  <div class="f-data">
                        <label><?php echo $this->translate('article_language'); ?>:</label>
				<?php echo $this->languageSelector(); ?>
                  </div>
                  <div class="f-data" >
                        <label><?php echo $this->translate('article_list_search_category'); ?>:</label>
                        <div class="translatable">
                        <?php
                        echo $this->helperLoader('category')
                                ->categorySelect(array(
                                                        'id'       => 'category',
                                                        'name'     => 'category',
                                                        'selected' => (isset($this->exp['category_id']) ? $this->exp['category_id'] : null)
                                                ), null, 'news');
                        ?>
                        </div>
                  </div>
                  <div class="f-data">
                        <label><?php echo $this->translator('article_list_search_status'); ?>:</label>
				<select name="status" style="width: 180px">
					<option value=""<?php if (!isset($this->exp['status'])) : ?> selected="selected"<?php endif; ?>>---</option>
					<option value="actived"<?php if (isset($this->exp['status']) && $this->exp['status'] == 'actived') : ?> selected="selected"<?php endif; ?>><?php echo $this->translate('article_list_status_actived'); ?></option>
					<option value="inactived"<?php if (isset($this->exp['status']) && $this->exp['status'] == 'inactived') : ?> selected="selected"<?php endif; ?>><?php echo $this->translate('article_list_status_inactived'); ?></option>
					<option value="drafted"<?php if (isset($this->exp['status']) && $this->exp['status'] == 'drafted') : ?> selected="selected"<?php endif; ?>><?php echo $this->translate('article_list_status_drafted'); ?></option>
					<option value="deleted"<?php if (isset($this->exp['status']) && $this->exp['status'] == 'deleted') : ?> selected="selected"<?php endif; ?>><?php echo $this->translate('article_list_status_deleted'); ?></option>
				</select>
                  </div>
                  <div  class="f-data">
                        <label>&nbsp;</label>
				<input type="checkbox" name="findMyArticles"<?php if (isset($this->exp['created_user_id'])) : ?> checked="checked"<?php endif; ?> value="1" /> <?php echo $this->translate('article_list_search_my_articles'); ?>
                  </div>
                  <div class="f-submit">
                      <input type="submit" class="button" value="<?php echo $this->translate('article_list_search_button'); ?>" />
                  </div>
              <div class="f-status">
                    <ul>
                          <li<?php if (!isset($this->exp['status'])) : ?> class="f-s-actived"<?php endif; ?>>
                                <a href="<?php echo $this->linkFilter($this->exp); ?>"><?php echo $this->translator('article_list_filter_all'); ?></a>
                          </li>
                          <li<?php if (isset($this->exp['status']) && $this->exp['status'] == 'actived') : ?> class="f-s-actived"<?php endif; ?>>
                                <a href="<?php echo $this->linkFilter($this->exp, 'actived'); ?>"><?php echo $this->translator('article_list_filter_actived'); ?></a>
                          </li>
                          <li<?php if (isset($this->exp['status']) && $this->exp['status'] == 'inactived') : ?> class="f-s-actived"<?php endif; ?>>
                                <a href="<?php echo $this->linkFilter($this->exp, 'inactived'); ?>"><?php echo $this->translator('article_list_filter_inactived'); ?></a>
                          </li>
                          <li<?php if (isset($this->exp['status']) && $this->exp['status'] == 'drafted') : ?> class="f-s-actived"<?php endif; ?>>
                                <a href="<?php echo $this->linkFilter($this->exp, 'drafted'); ?>"><?php echo $this->translator('article_list_filter_drafted'); ?></a>
                          </li>
                          <li<?php if (isset($this->exp['status']) && $this->exp['status'] == 'deleted') : ?> class="f-s-actived"<?php endif; ?>>
                                <a href="<?php echo $this->linkFilter($this->exp, 'deleted'); ?>"><?php echo $this->translator('article_list_filter_trash'); ?></a>
                          </li>
                    </ul>
                </div>
          </div>
            
           <table class="list" cellspacing="0">
			<thead>
				<tr>
					<td width="3%">
						<input type="checkbox" class="chkAll"/>
					</td>
					<td width="35%">
						<?php echo $this->translate('article_title'); ?>
					</td>
                              <td width="15%">
						<?php echo $this->translate('article_category'); ?>
					</td>
					<td width="15%">
						<?php echo $this->translate('article_status'); ?>
					</td>
					<td width="10%">
						<?php echo $this->translate('article_created_date'); ?>
					</td>
                              <td width="5%">
                                  <?php echo $this->languageSwitcher('flag', str_repeat('&nbsp', 2)); ?>
                              </td>
					<td width="15%">
						<?php echo $this->translate('article_actions'); ?>
					</td>
				</tr>
			</thead>
			<tbody>
			<?php
			if($this->numArticles == 0){
			?>
				<tr>
					<td colspan="7" style="color: #fc0202;">
						<?php echo $this->translate('article_list_not_found'); ?>
					</td>
				</tr>
			<?php
			}else{
				$i=0;
				foreach ($this->articles as $article){
			?>
				<tr id="article-<?php echo $article->id ?>" <?php if($i == $this->numArticles - 1) echo 'class="last"';?>>
					<td>
						<input type="checkbox" class="chkId" value = "<?php echo $article->id;?>"/>
					</td>
					<td>
						<a href="<?php echo  $this->url(array('id' => $article->id),'news_article_edit');?>"><?php echo $article->title?></a>

					</td>
					<td>
                                    <?php echo $article->category_name;?>
					</td>
                              <td>
                                    <?php
                                        echo $this->translate('article_list_status_'.$article->status);
  
                                    ?>
                              </td>
					<td>
						<?php echo date('d-m-Y', strtotime($article->created_date));?>
					</td>
					<td>
                                <?php if (($translations = $this->translationItems($article)) != null) : ?>
                                    <?php foreach ($translations as $language => $translation) : ?>
                                      <?php if ($translation == null && $language != $article->language) : ?>
                                            <?php if (isset($translations[$this->APP_DEFAULT_LANG])) : ?>
                                  
                                            <a class="a_helper_action_add" href="<?php echo $this->url(array('source_id' => $translations[$this->APP_DEFAULT_LANG]->id), 'news_article_add_localization'); ?>/<?php echo $language; ?>/">&nbsp;</a>
                                            <?php else :?>
                                            <a class="a_helper_action_add" href="<?php echo $this->url(array(), 'news_article_add'); ?>/<?php echo $language; ?>/">&nbsp;</a>
                                            <?php endif; ?>

                                    <?php elseif (($translation == null && $language == $article->language) || ($translation != null && $translation->id == $article->id)) : ?>
                                    <a class="a_helper_action_edit" href="<?php echo $this->url(array('id' => $article->id), 'news_article_edit'); ?>/<?php echo $language; ?>/">&nbsp;</a>

                                    <?php elseif ($translation != null && $translation->id != $article->id) : ?>
                                    <a class="a_helper_action_edit" href="<?php echo $this->url(array('id' => $translation->id), 'news_article_edit'); ?>/<?php echo $language; ?>/">&nbsp;</a>
                                        <?php endif; ?>
                            <?php endforeach; ?>
                        <?php else : ?>
                        &nbsp;
                        <?php endif; ?>
					</td>
					<td>
                                  <?php
                                    if($this->allow('edit')) {
                                  ?>
                                  <a href="<?php echo  $this->url(array('id' => $article->id),'news_article_edit');?>"><?php echo $this->translate('article_list_action_edit')?></a>
                                  <?php
                                    }
                                    if ($this->allow('activate')){
                                        if($article->status != 'actived'){
                                  ?>
                                  | <a href="<?php  echo  $this->url(array('id' => $article->id),'news_article_activate');?>"><?php echo $this->translate('article_list_action_activate')?></a>
                                  <?php
                                        }else{
                                  ?>
                                  | <a href="<?php echo  $this->url(array('id' => $article->id),'news_article_activate');?>"><?php echo $this->translate('article_list_action_deactivate')?></a>
                                  <?php
                                        }
                                    }
                                    if($this->allow('delete')){
                                  ?>
						| <a class="deleteAction" href="#" rel="<?php echo $article->id;?>"><?php echo $this->translate('article_list_action_delete')?></a>
                                  <?php
                                    }
                                    ?>
					</td>
				</tr>
			<?php
					$i++;
				}
			}?>
			</tbody>
			<tfoot>
				<tr>
					<td>
						<input type="checkbox" class="chkAll"/>
					</td>
					<td>
						<?php echo $this->translate('article_title'); ?>
					</td>
                              <td>
						<?php echo $this->translate('article_category'); ?>
					</td>
					<td>
						<?php echo $this->translate('article_status'); ?>
					</td>
					<td>
						<?php echo $this->translate('article_created_date'); ?>
					</td>
                              <td>
                                  <?php echo $this->languageSwitcher('flag', str_repeat('&nbsp', 2)); ?>
                              </td>
					<td>
						<?php echo $this->translate('article_actions'); ?>
					</td>
				</tr>
			</tfoot>
		</table>
	</form>
    
    <div id="pagination">
        <?php echo $this->paginator()->slide($this->paginator, $this->paginatorOptions); ?>
    </div>
</div>
<script type="text/javascript">
$(document).ready(function(){
        //delete Actin
      $(".deleteAction").click(function(){
        if(confirm("<?php echo $this->translate('article_list_action_delete_confirm')?>")){
            var id = $(this).attr("rel");
            $.ajax({
                type: 'POST',
                url: "<?php echo $this->url(array(), 'news_article_delete'); ?>",
                data: { id: id },
                success: function(response) {
                    if ('RESULT_OK' == response) {
                        $('#article-' + id).fadeOut('slow');
                    }
                }
            });
        }
        return false;
      });
	//xóa
	$("#btnDelete").click(function(){
		var arrChkId = $(".chkId");
		var ids = new Array();
		for(var i =0; i < arrChkId.length; i++){
			if(arrChkId.eq(i).attr("checked")){
                      ids.push(arrChkId.eq(i).val());
			}
		}
            if(ids.length > 0){
			if(confirm("<?php echo $this->translate('article_list_action_delete_many_confirm') ?>")){
				$.ajax({
                            type: 'POST',
                            url: "<?php echo $this->url(array(), 'news_article_delete'); ?>",
                            data: { id: $.toJSON(ids) },
                            success: function(response) {
                                if ('RESULT_OK' == response) {
                                    for(var i in ids)
                                        $('#article-' + ids[i]).fadeOut('slow');
                                }
                            }
                        });
			}else{
				return false;
			}
		}else{
			alert("<?php echo $this->translate('article_list_bulk_action_none_selected')?>");
			return false;
		}
	});
});
</script>
